package com.partners.controller;

import com.partners.annotation.SimpleParam;
import com.partners.dao.dto.UserTestDTO;
import com.partners.model.ApiResult;
import com.partners.model.Member;
import com.partners.utils.ApiResultUtil;
import com.partners.utils.TokenSession;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.UUID;

/**
 * @author liangyongle
 * @date 2019-01-15
 */

@RestController
@RequestMapping("test")
@Slf4j
public class TestController {


    @Resource
    private TokenSession tokenSesson;

    @PostMapping("token")
    public ApiResult tokenTest(@RequestBody UserTestDTO userTestDTO){
        return ApiResultUtil.ok(userTestDTO);
    }

    /**
     * 测试缓存序列化
     */
    @PostMapping("test")
    public ApiResult SessionMange(){

        Member member = new Member();
        member.setToken("adadasasds");
        member.setOpenId("kasdkajdkajdasd");
        member.setName("Forever");
        member.setId(123456);
        String token = UUID.randomUUID().toString();
        log.info("token:{}",token);

        tokenSesson.updateMemberTokenContent(token,member);

        return ApiResultUtil.ok(token);
    }

    @PostMapping("testGet")
    public ApiResult getSession(@SimpleParam("token") String token){
        Member member =  tokenSesson.getMemberTokenContent(token);

        return ApiResultUtil.ok(member);
    }

}
